// 工具类：


//网络请求
async function fetchSup(url, callback) {
	let r = await fetch(url);
	let r2 = await r.json()
	callback(r2)
}


// 1 Loading
var Loading = {
	element: null,
	inter: null,
	"show": function() {
		this.element = document.createElement("div");
		this.element.style =
			"position: fixed; z-index:999999; text-align: center; left:0; right:0; top:0; bottom:0; opacity: 0.3; background-color:black"
		this.element.innerHTML =
			'<img src="./img/loading.png" alt="" style="position: absolute; bottom: 50px;">'
		document.body.appendChild(this.element);
		var deg = 0;
		this.inter = setInterval(function() {
			deg += 5;
			Loading.element.firstElementChild.style.transform = "rotate(" + deg + "deg)";
		}, 20)

	},
	"hide": function() {
		this.element.remove();
		clearInterval(this.inter);
		this.element = null;
	}
}


// 2 Toast 轻提示
var Toast = {
	ele: null,
	myTim: null,
	"show": function(msg, time) {
		if (this.ele) {
			this.ele.remove();
			if (this.myTim) {
				clearTimeout(this.myTim);
			}
		}
		this.ele = document.createElement("div");
		this.ele.style =
			'position: absolute; z-index:9999; left: 0; right: 0; bottom: .5rem; display: flex; justify-content: center;';
		var text = document.createElement("span");
		text.innerText = msg;
		text.style = 'background-color: lightgrey; display: inline-block; padding: 5px; border-radius: 5px;';
		this.ele.appendChild(text);
		document.body.appendChild(this.ele);
		this.myTim = setTimeout(function() {
			Toast.ele.remove();
		}, time)
	}
}


// 3 钟表
var Clock = {
	"init": function() {
		var outline = document.querySelector(".clock .lines");

		function drawLine() {
			for (var i = 0; i < 60; i++) {
				var line = document.createElement("div");
				line.style.transform = "rotate(" + i * 6 + "deg)"
				line.classList.add('line');
				outline.appendChild(line);
			}
		}
		drawLine();

		var timeEle = document.querySelector(".clock .types");

		setInterval(function() {
			var now = new Date();
			timeEle.children[2].style.transform = "rotate(" + (now.getSeconds() * 6 - 90) + "deg)";

			timeEle.children[1].style.transform = "rotate(" + ((now.getMinutes() + now.getSeconds() /
				60) * 6 - 90) + "deg)";

			timeEle.children[0].style.transform = "rotate(" + ((now.getHours() + now.getMinutes() / 60 +
				now.getSeconds() / 3600) * 30 - 90) + "deg)";

		}, 1000);
	}
}


function timeFormat() {
	var now = new Date();
	return `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
}


// 设置cookie
function setCookie(cname, cvalue, exdays) {

	var d = new Date();

	d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));

	var expires = "expires=" + d.toUTCString();

	document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";

}
